package com.vitas.coin

import android.app.Activity
import android.os.Bundle
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatDelegate
import com.huawei.agconnect.AGConnectInstance
import com.huawei.hms.audioeditor.common.agc.HAEApplication
import com.snap.base.base.BaseApp
import com.snap.common.config.AdConfig
import com.snap.common.config.AppConfig
import com.snap.common.plugin.AppPlugin
import com.snap.umeng.plugin.UMPlugin
import com.snap.wx.plugin.WXPlugin
import com.vitas.coin.constant.AppConstant
import com.vitas.coin.manager.PlayManager
import com.vitas.coin.ui.act.SplashAct
import com.vitas.coin.util.Audio


class MyApp : BaseApp() {

    var act = ArrayList<Activity>()

    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(
                activity: Activity,
                @Nullable savedInstanceState: Bundle?
            ) {
                act.add(0, activity)
            }

            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityResumed(activity: Activity) {
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            }

            override fun onActivityDestroyed(activity: Activity) {
                act.remove(activity)
            }
        })
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        PlayManager.getInstance().initPlayer(this)
    }

    fun getTopAct(): Activity? {
        if (act.isEmpty()) {
            return null
        }
        return act[0]
    }

    override fun getSplash(): Class<*> {
        return SplashAct::class.java
    }

    override fun getHttpBaseUrl(): String {
        return AppConstant.APP_BASE_URL
    }

    override fun addPlugin(plugins: MutableList<AppPlugin>) {
        super.addPlugin(plugins)
        if (BuildConfig.DEBUG) {
            var it = plugins.iterator()
            while (it.hasNext()) {
                var tmp = it.next()
                if (tmp is UMPlugin) {
                    it.remove()
                    break
                }
            }
        }
        // 微信
        plugins.add(WXPlugin(AppConstant.ID_WECHAT_APP_ID, AppConstant.ID_WECHAT_SECRET))
    }

    override fun setAppConfig() {
        AppConfig.apply {
            privacy = BuildConfig.app_privacy
            agreement = BuildConfig.app_agreement
            channel = BuildConfig.CHANNEL
            appKey = AppConstant.APP_KEY
            umID = AppConstant.ID_UMENG
            supportBuy = true

            ad = AdConfig(
                appId = AppConstant.AD_APP_ID,
                appKey = AppConstant.AD_APP_KEY,
                splash = AppConstant.AD_SPLASH,
                insert = AppConstant.AD_INSERT,
                banner = AppConstant.AD_BANNER,
                reward = AppConstant.AD_REWARD,
                native = AppConstant.AD_NATIVE,

                checkLocal = !BuildConfig.DEBUG,
                close = false
            )
        }
    }

    override fun onAfterAgree() {
        super.onAfterAgree()
        val apiKey = AGConnectInstance.getInstance().getOptions().getString("client/api_key")
        HAEApplication.getInstance().setApiKey(apiKey);
        Audio.setCallback()
    }
}